package encome.work.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import encome.work.dao.Supplier;
import encome.work.dao.ResponseResult;
import encome.work.utils.StatusCode;
import encome.work.service.SupplierService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Resource
    private SupplierService supplierService;

    /**
     * 新增需求表
     */
    @PostMapping("/insert")
    public ResponseResult<String> add(@RequestBody Supplier supplier){
        supplierService.add(supplier);
        return new ResponseResult<>(StatusCode.OK, "新增需求表成功");
    }

    /**
     * 更新需求表
     */
    @PutMapping("/update/{id}")
    public ResponseResult<String> update(@PathVariable("id") Long id, @RequestBody Supplier supplier){
        supplier.setId(id);
        supplierService.update(supplier);
        return new ResponseResult<>(StatusCode.OK, "修改需求表成功");
    }

    /**
     * 删除需求表
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult<String> delete(@PathVariable("id") Long id){
        supplierService.delete(id);
        return new ResponseResult<>(StatusCode.OK, "删除需求表成功");
    }

    /**
     * 多条件分页查询需求表
     */
    @PostMapping("/search/{page}/{size}")
    public ResponseResult<Page<Supplier>> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size,
                                                     @RequestBody Supplier supplier){
        Page<Supplier> supplierPage= supplierService.findPage(supplier, page, size);
        return new ResponseResult<>(StatusCode.OK, "查询成功", supplierPage);
    }
    /**
     * 根据id查询需求表
     */
    @GetMapping("/{id}")
    public ResponseResult<Supplier> findById(@PathVariable("id") Long id){
        Supplier supplier = supplierService.findById(id);
        return new ResponseResult<>(StatusCode.OK, "查询成功", supplier);
    }

    /**
     * 默认分页查询需求表
     */
    @GetMapping("/search/{page}/{size}")
    public ResponseResult<Page<Supplier>> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Page<Supplier> supplierPage = supplierService.findPage(page, size);
        return new ResponseResult<>(StatusCode.OK,"查询成功", supplierPage);
    }
}
